<template>
  <div>
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item :to="{ path: '/home' }">首页</el-breadcrumb-item>
      <el-breadcrumb-item>用户管理</el-breadcrumb-item>
      <el-breadcrumb-item>用户列表</el-breadcrumb-item>
    </el-breadcrumb>
    <el-card class="box-card">
      <!-- 布局响应式的布,页面的宽分成24个格 列之间的宽度 -->
      <el-row :gutter='10'>
        <el-col :span="8">
          <el-input placeholder="请输入内容" v-model="queryInfo.query">
            <el-button slot="append" class="el-icon-search"> </el-button>
          </el-input>
        </el-col>
        <el-col :span="2">
          <el-button type="primary">添加用户</el-button>
        </el-col>
      </el-row>
      <el-table :data="tableData" border style="width: 100%">
        <el-table-column type="index" label="序号"></el-table-column>
        <el-table-column prop="username" label="姓名"></el-table-column>
        <el-table-column prop="email" label="邮箱"> </el-table-column>
        <el-table-column prop="mobile" label="电话"></el-table-column>
        <el-table-column prop="role_name" label="角色"> </el-table-column>
        <el-table-column label="状态">
          <!-- 用插槽来获取数据 -->
          <template slot-scope="socope">
            <el-switch v-model="socope.row.mg_state" active-color="#13ce66"
                       inactive-color="#ff4949">
            </el-switch>
          </template>
        </el-table-column>
        <el-table-column label="操作"></el-table-column>
      </el-table>

      <el-pagination @size-change="handleSizeChange"
                     @current-change="handleCurrentChange"
                     :current-page="queryInfo.pagenum"
                     :page-sizes="[100, 200, 300, 400]"
                     :page-size="queryInfo.pagesize"
                     layout="total, sizes, prev, pager, next, jumper"
                     :total="total">
      </el-pagination>
    </el-card>
    <!-- 添加用户 -->
    <el-dialog title="添加用户" class="mydialog"
               :visible.sync="dialogFormVisibleAdd" @close='addFormClose'>
      <el-form ref="ruleFormRef" :model="addForm" :rules="rules"
               label-width="80px" class="demo-ruleForm">
        <el-form-item label="用户名" prop="username">
          <el-input v-model="addForm.username"></el-input>
        </el-form-item>
        <el-form-item label="密码" prop="password">
          <el-input v-model="addForm.password"></el-input>
        </el-form-item>
        <el-form-item label="邮箱" prop="email">
          <el-input v-model="addForm.email"></el-input>
        </el-form-item>
        <el-form-item label="手机号" prop="mobile">
          <el-input v-model="addForm.mobile"></el-input>
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button @click="dialogFormVisibleAdd = false">取 消</el-button>
        <el-button type="primary" @click="dialogFormVisibleAdd = false">确 定
        </el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
export default {
  mounted () {
    this.getUserList()
  },
  data () {
    return {
      // 控制dialog的显隐
      dialogFormVisibleAdd: false,
      // 里边的表单
      addForm: {
        username: '',
        password: '',
        email: '',
        mobile: ''
      },
      // 校验规则,:rules来绑定
      rules: {
        username: [
          { required: true, message: '必填的,可以写别的', trigger: 'blur' },
          { min: 3, max: 15, message: '长度在 3 到 5 个字符', trigger: 'blur' },
        ],
        password: [
          { required: true, message: '必填的,可以写别的', trigger: 'blur' },
          { min: 3, max: 6, message: '长度在 3 到 5 个字符', trigger: 'blur' }
        ],
        email: [
          { required: true, message: '必填的,可以写别的', trigger: 'blur' },
          // alt + ↓
          { pattern: /^\w+([-+.]\w+)*@\w+([-.]\w+)*\.\w+([-.]\w+)*$/, message: '请输入正确的格式', trigger: 'blur' }
        ],
        mobile: [
          { required: true, message: '必填的,可以写别的', trigger: 'blur' },
          // alt + ↓
          { pattern: /^(13[0-9]|14[5|7]|15[0|1|2|3|5|6|7|8|9]|18[0|1|2|3|5|6|7|8|9])\d{8}$/, message: '请输入正确的格式', trigger: 'blur' }
        ]
      },
      queryInfo: {
        query: '',
        pagenum: 1,
        pagesize: 2
      },
      total: 0,
      tableData: []
    }
  },
  methods: {
    async getUsers () {
      // es6的解构
      const { data: { data, meta } } = await this.$axios.get('users', { params: this.queryInfo })
      if (meta.status !== 200) {
        this.$message.error('获取用户信息失败')
        return
      }
      this.tableData = data.users
      this.total = data.total
      console.log(data, meta)
    },
    handleSizeChange (size) {
      console.log('改变的查询的条数', size)
      this.queryInfo.pagesize = size;
      // 页面的数据重新的渲染
      this.getUsers()
    },
    handleCurrentChange (page) {
      console.log('改变的页数', page)
      this.queryInfo.pagenum = page;
      // 页面的数据重新的渲染
      this.getUsers()
    }
  }
}
</script>

<style lang="css" scoped>
</style>
